﻿using CoreHelper;
using CoreHelper.Reflection;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreWeb.Api
{
    public static class SignTools
    {
        public static string GetSign(object requestParams, string app_secret, out string plainText)
        {
            var tci = TypeCachedManager.GetTypeCachedInfo(requestParams.GetType());

            SortedDictionary<string, string> sortedDictionary = new SortedDictionary<string, string>();
            foreach (var kv in tci.PropInvokerDict)
            {
                var signParam = kv.Value.Property.GetCustomAttributes(typeof(SignParamAttribute), false).FirstOrDefault() as SignParamAttribute;
                if (signParam != null && signParam.Ignore)
                {
                    continue;
                }
                if (kv.Value.Property.PropertyType.IsArray) continue;
                if (typeof(IList).IsAssignableFrom(kv.Value.Property.PropertyType)) continue;

                var value = kv.Value.Get(requestParams);
                string strValue = "";
                if (value != null)
                {
                    strValue = value.ToString().ToLower();
                }
                //日期格式，用于签名
                if (kv.Value.Property.PropertyType == typeof(DateTime))
                {
                    if (signParam != null)
                    {
                        strValue = ((DateTime)value).ToString(signParam.Format);
                    }
                    else
                    {
                        strValue = ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
                if (kv.Value.Property.PropertyType == typeof(TimeSpan))
                {
                    if (signParam != null)
                    {
                        strValue = ((TimeSpan)value).ToString(signParam.Format);
                    }
                    else
                    {
                        strValue = ((TimeSpan)value).ToString(@"hh\:mm\:ss");
                    }
                }
                else if (kv.Value.Property.PropertyType == typeof(decimal))
                {
                    if (signParam != null)
                    {
                        strValue = ((decimal)value).ToString(signParam.Format);
                    }
                    else
                    {
                        strValue = ((decimal)value).ToString("0.00");
                    }
                }
                sortedDictionary.Add(kv.Key.ToLower(), strValue);
            }

            List<string> partList = new List<string>();
            foreach (var item in sortedDictionary)
            {
                partList.Add(item.Key + "=" + item.Value);
            }
            partList.Add("app_secret=" + app_secret.ToLower());
            var ps = StringHelper.JoinStr(partList, "&");
            plainText = ps;
            var mySign = EncryptHelper.MD5Encrypt(ps);
            return mySign;
        }
    }
}
